# Contents: Ftp_File_Manager module
# Purpose: To handle all FTP related operations
# Status: 

require  'socket'
require  'net/ftp'
require  'Song'
require  'Library'
			
module Ftp_File_Mananger

public
# 1 is up load 0 is download

	def Ftp_File_Mananger.initialize()
		init_ftp()
	end 
	def Ftp_File_Mananger.ftp_configure(configurations)
		if configuration.length != 8 then 
			print "not enough information\n"
			return nil
		end
		config_file = File.open("FTP.config",RDWR)
		if config_file == nil then
			config_file = File.open("FTP.config",CREAT|RDWR)
			i = 0
			while (i < 8)
				configuration[i] << "-\n"
				config_file.write(configs[i])
				i = i + 1
			end
			config_file.close
			return
		end
		configs = config_file.readlines
		i = 0 
		config_file.truncate("FTP.config",0)
		while (i < 8)
			if configuration[i] != "-" then
				configs[i] = configuration[i]
			end
			configs[i] << "\n"
			config_file.write(configs[i])
		end		
	end
	def Ftp_File_Mananger.ftp_startup(flag) 
		ftp = connecting_ftp()
		if ftp == nil then 
			print "failure to connect to ftp\n"
		end
		ftp = login_ftp(ftp)
		if ftp == nil then 
			print "failure to login in to ftp\n"		
		end
		if flag then 						
			error = down_ftp(ftp)
			if error == nil then 
				print "failure to to download from ftp\n"		
			end
		elsif flag == 0 then
			error = up_ftp(ftp)
			if error == nil then 
				print "failure to to upload from ftp\n"		
			end
		end
		ftp.close
	return 1
	end
	def Ftp_File_Mananger.get_time
		@time_interval_update
	end
	def Ftp_File_Mananger.get_last_time
		@time_of_last_update
	end
	def Ftp_File_Mananger.get_local_lib_loc
		@local_lib_loc
	end
	def Ftp_File_Mananger.get_local_lib_name
		@local_lib_name
	end
	def Ftp_File_Mananger.get_remote_pw
		@remote_login_pw
	end
	def Ftp_File_Mananger.get_remote_login_name
		@remote_login_name
	end
	def Ftp_File_Mananger.get_remote_loc
		@remote_loc
	end
	def Ftp_File_Mananger.get_remote_dir
		@remote_dir
	end
	def Ftp_File_Mananger.get_remote_name
		@remote_name
	end
	def Ftp_File_Mananger.get_all
		ret = Array.new(9)
		ret[0] = @time_interval_update
		ret[1] = @time_of_last_update
		ret[2] = @local_lib_loc
		ret[3] = @local_lib_name
		ret[4] = @remote_login_pw
		ret[5] = @remote_login_name
		ret[6] = @remote_loc
		ret[7] = @remote_dir
		ret[8] = @remote_name
		return ret
	end
	# the will connect to the server returning the 
	#server connect
	def init_ftp()
		config_file = File.new("FTP.config","r")
		if config_file == nil then
			config_file = File.open("FTP.config",CREAT|RDWR)
			i = 0
			while(i<10)
				config_file.write("\n")
			end
			config_file.close
			return
		end
		configs = config_file.readlines
		if configs.length < 9 then 
#			somthing will happen
		end
		@remote_name = configs[0]
		@remote_dir = configs[1]
		@remote_loc = configs[2]
		@remote_login_name = configs[3]
		@remote_login_pw = configs[4]
		@local_lib_name = configs[5]
		@local_lib_loc = configs[6]
		@time_of_last_update = configs[7]
		@time_interval_update = configs[8]
		configs.close
	end
private
#connects to ftp
	def connecting_ftp()
		if @remote_loc == nil then
			print "no remote_loc set cannot connect\n"
			return nil
		end
		ftp = Net::FTP.new()
		i = 0
			begin
				ftp.connect(@remote_loc,FTP_PORT)
			rescue	Errno::ECONNREFUSED
					print "could not connect to the server"	
					return nil			
			end
		return ftp
	end
# login into the ftp server
	def login_ftp(ftp)
		if @remote_login == nil or remote_login_pw == nil then 
			print "cannot login into the ftp no user or password set"
			return nil 
		end
		log_ftp = ftp
		begin 
			log_ftp.login(@remote_login_name,@remote_login_pw)
		rescue Net::FTPPermError
			print "could not lob into the server"
			return nil
		end
		return log_ftp
	end
#downloads from ftp
	def down_ftp(ftp)
		if @remote_dir == nil or @remote_name == nil or @remote_lib_name == nil then
			print "do not know what directory or file to grab or were to place"
			return nil
		end
			ftp.chdir(@remote_dir)
			file = ftp.gettextfile(@remote_name,@local_lib_name,nil)	
	end
#uploads from ftp
	def up_ftp(ftp)
		if @remote_dir == nil or @remote_name == nil or @remote_lib_name == nil then
			print "do not know what directory or file to grab or were to place"
			return nil
		end
			ftp.chdir(@remote_dir)
			ftp.puttextfile(@local_lib_name,@remote_lib_name,nil)
	end
end
